home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1998 July / EnigmA AMIGA RUN 29 (1998)(G.R. Edizioni)(IT)[!][issue 1998-07 & 08].iso / earcd / phase5 / lha-ppc / src / shuf.c < prev    next >
C/C++ Source or Header  |  1997-12-04  |  3KB  |  172 lines

  1. /***********************************************************
  2.     shuf.c -- extract static Huffman coding
  3. ***********************************************************/
  4. #include <stdio.h>
  5. #include "slidehuf.h"
  6. #ifdef __STDC__
  7. #include <stdlib.h>
  8. #endif
  9.  
  10. #define N1 286  /* alphabet size */
  11. #define N2 (2 * N1 - 1)  /* # of nodes in Huffman tree */
  12. #define EXTRABITS 8
  13.     /* >= log2(F-THRESHOLD+258-N1) */
  14. #define BUFBITS  16  /* >= log2(MAXBUF) */
  15. #define LENFIELD  4  /* bit size of length field for tree output */
  16. #define NP (8 * 1024 / 64)
  17. #define NP2 (NP * 2 - 1)
  18.  
  19. static unsigned int np;
  20.  
  21. void decode_start_st0(void)
  22. {
  23.     n_max = 286;
  24.     maxmatch = MAXMATCH;
  25.     init_getbits();
  26.     np = 1 << (MAX_DICBIT - 6);
  27. }
  28.  
  29. void encode_p_st0(unsigned short j)
  30. {
  31.     unsigned short i;
  32.  
  33.     i = j >> 6;
  34.     putcode(pt_len[i], pt_code[i]);
  35.     putbits(6, j & 0x3f);
  36. }
  37.  
  38. int fixed[2][16] = {
  39.     {3, 0x01, 0x04, 0x0c, 0x18, 0x30, 0},                /* old compatible */
  40.     {2, 0x01, 0x01, 0x03, 0x06, 0x0D, 0x1F, 0x4E, 0}    /* 8K buf */
  41. };
  42.  
  43. static void ready_made(int method)
  44. {
  45.     int i, j;
  46.     unsigned int code, weight;
  47.     int *tbl;
  48.  
  49.     tbl = fixed[method];
  50.     j = *tbl++;
  51.     weight = 1 << (16 - j);
  52.     code = 0; 
  53.     for (i = 0; i < np; i++) {
  54.         while (*tbl == i) {
  55.             j++;
  56.             tbl++;
  57.             weight >>= 1;
  58.         }
  59.         pt_len[i] = j;
  60.         pt_code[i] = code;
  61.         code += weight;
  62.     }
  63. }
  64.  
  65. void encode_start_fix(void)
  66. {
  67.     n_max = 314;
  68.     maxmatch = 60;
  69.     np = 1 << (12 - 6);
  70.     init_putbits();
  71.     start_c_dyn();
  72.     ready_made(0);
  73. }
  74.  
  75. static void read_tree_c(void)  /* read tree from file */
  76. {
  77.     int i, c;
  78.  
  79.     i = 0;
  80.     while (i < N1) {
  81.         if (getbits(1)) c_len[i] = getbits(LENFIELD) + 1;
  82.         else           c_len[i] = 0;
  83.         if (++i == 3 && c_len[0] == 1 && c_len[1] == 1 && c_len[2] == 1) {
  84.             c = getbits(CBIT);
  85.             for (i = 0; i < N1; i++) c_len[i] = 0;
  86.             for (i = 0; i < 4096; i++) c_table[i] = c;
  87.             return;
  88.         }
  89.     }
  90.     make_table(N1, c_len, 12, c_table);
  91. }
  92.  
  93. static void read_tree_p(void)  /* read tree from file */
  94. {
  95.     int i, c;
  96.  
  97.     i = 0;
  98.     while (i < NP) {
  99.         pt_len[i] = getbits(LENFIELD);
  100.         if (++i == 3 && pt_len[0] == 1 && pt_len[1] == 1 && pt_len[2] == 1) {
  101.             c = getbits(MAX_DICBIT - 6);
  102.             for (i = 0; i < NP; i++) c_len[i] = 0;
  103.             for (i = 0; i < 256; i++) c_table[i] = c;
  104.             return;
  105.         }
  106.     }
  107. }
  108.  
  109. void decode_start_fix(void)
  110. {
  111.     n_max = 314;
  112.     maxmatch = 60;
  113.     init_getbits();
  114.     np = 1 << (12 - 6);
  115.     start_c_dyn();
  116.     ready_made(0);
  117.     make_table(np, pt_len, 8, pt_table);
  118. }
  119.  
  120. unsigned short decode_c_st0(void)
  121. {
  122.     int i;
  123.     unsigned short j;
  124.     static unsigned short blocksize = 0;
  125.  
  126.     if (blocksize == 0) {  /* read block head */
  127.         blocksize = getbits(BUFBITS);  /* read block blocksize */
  128.         read_tree_c();
  129.         if (getbits(1)) {
  130.             read_tree_p();
  131.         } else {
  132.             ready_made(1);
  133.         }
  134.         make_table(NP, pt_len, 8, pt_table);
  135.     }
  136.     blocksize--;
  137.     j = c_table[bitbuf >> 4];
  138.     if (j < N1) fillbuf(c_len[j]);
  139.     else {
  140.         fillbuf(12); i = bitbuf;
  141.         do {
  142.             if ((short)i < 0) j = right[j];
  143.             else              j = left [j];
  144.             i <<= 1;
  145.         } while (j >= N1);
  146.         fillbuf(c_len[j] - 12);
  147.     }
  148.     if (j == N1 - 1)
  149.         j += getbits(EXTRABITS);
  150.     return j;
  151. }
  152.  
  153. unsigned short decode_p_st0(void)
  154. {
  155.     int i;
  156.     unsigned short j;
  157.  
  158.     j = pt_table[bitbuf >> 8];
  159.     if (j < np) {
  160.         fillbuf(pt_len[j]);
  161.     } else {
  162.         fillbuf(8); i = bitbuf;
  163.         do {
  164.             if ((short)i < 0) j = right[j];
  165.             else              j = left [j];
  166.             i <<= 1;
  167.         } while (j >= np);
  168.         fillbuf(pt_len[j] - 8);
  169.     }
  170.     return (unsigned short)((j << 6) + getbits(6));
  171. }
  172.